package com.compass.mobile.ui.viewModel

import androidx.lifecycle.MutableLiveData
import com.compass.mobile.framework.utils.TipsToast
import com.compass.mobile.models.CaseHistory
import com.compass.mobile.models.MedicalRecordReport
import com.compass.mobile.models.PrescriptionRecord
import com.compass.mobile.network.callback.IApiErrorCallback
import com.compass.mobile.network.manager.ApiManager
import com.compass.mobile.network.viewmodel.BaseViewModel

/**
 * Description：个人中心
 * Date:2024/6/19 11:06
 */
class PersonalCenterViewModel : BaseViewModel() {

    val caseHistoryLiveData = MutableLiveData<MutableList<CaseHistory>?>()
    val caseHistoryPatientLiveData = MutableLiveData<MutableList<MedicalRecordReport>?>()
    val prescriptionsLiveData = MutableLiveData<MutableList<PrescriptionRecord>?>()
    val continuePrescriptionLiveData = MutableLiveData<Any?>()

    fun caseHistory(patientId: String?, pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.caseHistory(patientId, pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            caseHistoryLiveData.value = it
        })
    }

    fun continuePrescription(id: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.continuePrescription(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                continuePrescriptionLiveData.value=null
            }
        }, successBlock = {
            continuePrescriptionLiveData.value = it
        })
    }

    fun caseHistoryPatient(patientId: String?, pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.caseHistoryPatient(patientId, pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {

            if (it == null || it.list.isNullOrEmpty()) {
                caseHistoryPatientLiveData.value = null
            } else {
                caseHistoryPatientLiveData.value = it.list
            }
        })
    }


    fun prescriptionRecords(patientId: String?, status: String, pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.prescriptionRecords(patientId, pageNo, pageSize, status)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                prescriptionsLiveData.value = null
            } else {
                prescriptionsLiveData.value = it.list
            }

        })
    }
}